﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;
using TwitterMatic.Shared.Validation;
using Microsoft.WindowsAzure.StorageClient;

namespace TwitterMatic.Shared.Domain
{
    public class TimedTweet : TableServiceEntity, IComparable
    {
        public string Token { get; set; }
        public string TokenSecret { get; set; }

        [Required(ErrorMessage = "Twitter screen name is required.")]
        public string ScreenName { get; set; }

        [Required(ErrorMessage = "Message is required.")]
        [StringLength(140, ErrorMessage = "Message length must not exceed 140 characters.")]
        public string Status { get; set; }

        [Required(ErrorMessage = "A scheduled time is required.")]
        [CustomValidation(typeof(TimedTweetValidation), "ValidateFutureDate", ErrorMessage = "The scheduled time should be in the future.")]
        public DateTime SendOn { get; set; }
        public DateTime SentOn { get; set; }
        public string SendStatus { get; set; }
        public int RetriesLeft { get; set; }

        public bool Archived { get; set; }

        public TimedTweet()
            : base()
        {
            SendOn = DateTime.Now.ToUniversalTime();
            Timestamp = DateTime.Now;
            RowKey = Guid.NewGuid().ToString();
            SendStatus = "Scheduled";
            RetriesLeft = 10;
        }

        public TimedTweet(string partitionKey, string rowKey)
            : base(partitionKey, rowKey)
        {
            SendOn = DateTime.Now.ToUniversalTime();
            SendStatus = "Scheduled";
            RetriesLeft = 10;
        }

        public TimedTweet(string screenName)
            : base()
        {
            Timestamp = DateTime.Now;
            PartitionKey = screenName;
            RowKey = Guid.NewGuid().ToString();
            ScreenName = screenName;
            SendOn = DateTime.Now.ToUniversalTime();
            SentOn = new DateTime(2000, 1, 1).ToUniversalTime();
            SendStatus = "Scheduled";
            RetriesLeft = 10;
        }

        public TimedTweet(string screenName, string status, DateTime sendOn)
            : base()
        {
            Timestamp = DateTime.Now;
            PartitionKey = screenName;
            RowKey = Guid.NewGuid().ToString();
            ScreenName = screenName;
            SendOn = sendOn;
            SentOn = new DateTime(2000, 1, 1).ToUniversalTime();
            SendStatus = "Scheduled";
            RetriesLeft = 10;
        }

        #region IComparable Members

        public int CompareTo(object obj)
        {
            TimedTweet target = obj as TimedTweet;
            if (target != null) {
                return this.SendOn.CompareTo(target.SendOn);
            }
            return 0;
        }

        #endregion
    }
}
